/**
 * 
 */
package com.qq.spirit;

import java.util.List;

import org.apache.http.Header;

import com.qq.spirit.db.DBUtil;
import com.qq.spirit.inter.AbstractXmlActionAfterRequest;
import com.qq.spirit.inter.TaskUnit;
import com.qq.spirit.model.UserData;
import com.qq.spirit.model.XmlDataUtil;
import com.qq.spirit.model.XmlNodeData;

/**
 * @author beedoorwei
 * 
 */
public class GetUserInfoTask extends TaskUnit
{
	public GetUserInfoTask(int gid, TaskUnit a)
	{
		super();
		this.gid = gid;
		this.a = a;
	}

	public void doTask()
	{
		try
		{
			HttpUtil.doGet(gid, GameConstants.PLAY_USER_INFO_URL, new AfterGetUserAction(gid, a));
			
			if(a != null)
			{
				TaskManager.addTask(a);
			}
		} catch (Exception ex)
		{

		}
	}

	class AfterGetUserAction extends AbstractXmlActionAfterRequest
	{
		public AfterGetUserAction(int gid, TaskUnit a)
		{
			this.gid = gid;
			this.taskUnit = a;
		}

		@Override
		public void doCall(List<Header> headerList, XmlNodeData resultData)
		{
			UserData u = GameUserInfoUtil.getUser(gid);
			if (u.getGameInfo("game_nickname") == null)
			{
				
				DBUtil.executeUpdate("update u_game_user set game_nick_name='"+XmlDataUtil.getNodeData(resultData, "results.player.playername")+"',sessionid='"+u.getCookie("JSESSIONID")+"'");
				u.setGameInfo("game_nickname", XmlDataUtil.getNodeData(resultData, "results.player.playername"));
				u.setGameInfo("nation",XmlDataUtil.getNodeData(resultData, "results.player.nation"));
			}
		}
	}
}
